package TUILayer;
import java.util.Scanner;
import ControlLayer.*;

/**
 * Customer user interface:
 * Enables the user to create, find, delete , update and list all customers.
 * 
 * @author (Gruppe 3)
 */
public class CustomerUI
{
    // instance variables
    private PersonCtr personCtr;

   /**
    * Constructor for objects of class CustomerUI
    */
   public CustomerUI()
   {
      
      personCtr = new PersonCtr();
   }
   
   /**
     * Opens the customer menu
     */
   public void open()
    {
        customerMenu();
    }
    
    /** 
     * Calls the print customer menu method and let the user pick a choice for the menu
     */
    private void customerMenu()
    {
       boolean back = false;
       while(!back)
       {
           int choice = makeCustomerMenu();
           if(choice == 1)
           {
               //Create customer
               createCustomer();
            }
            else
            {
                if(choice == 2)
                {
                    //Find customer
                    findCustomer();
                }
                else
                {
                    if(choice == 3)
                    {
                        //Delete customer
                        deleteCustomer();
                    }
                    else
                    {
                        if(choice == 4)
                        {
                            //update customer
                            updateCustomer();
                        }
                        else
                        {
                            if(choice == 5)
                            {
                                //list all customers
                                showAllCustomers();
                            }
                            else
                            {
                                if(choice == 6)
                                {
                                    //Go back to previous menu
                                    back = true;
                                }
                                else
                                {
                                    System.out.println("Indtast et nummer mellem 1 og 6");
                                    waitInput();
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    
    /**
     * Prints customer menu
     */
    private int makeCustomerMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f ### Kunde menu ### ");
        System.out.println(" (1) Opret kunde");
        System.out.println(" (2) Find kunde");
        System.out.println(" (3) Slet kunde");
        System.out.println(" (4) Ret kunde");
        System.out.println(" (5) Vis alle kunder");
        System.out.println(" (6) Tilbage");
        System.out.println("\n Vælg et nummer");
        
        int choice = keyboard.nextInt();
        return choice;
    }
    
    /**
     * System waits for an input, so the user can read the screen information
     */
    private void waitInput()
    {
        System.out.println("Tryk på en tast for at forsætte");
        new Scanner(System.in).nextLine();
    }
    
    /**
     * input of a string
     */
    private String inputString()
    {
        Scanner keyboard = new Scanner(System.in);
        String newInput = keyboard.nextLine();
        return newInput;
    }
    
    /**
     * Checks if a string is a double and returns a boolean based on this
     */
        private boolean isDouble(String checkInput)
    {
        try
        {
            Double.parseDouble(checkInput);
        }
        catch(NumberFormatException e)
        {   
            System.out.println("Du skal skrive et heltal eller decimaltal.");
            return false;
        }
        
        return true;        
    }    
    
    /**
     * inputs a variable of the type double
     */
    private double inputDouble()
    {
        Scanner keyboard = new Scanner(System.in);      
        boolean parsedInput = false;
        
        while(!parsedInput)
        {   
            String testInput = keyboard.nextLine();
            
            // checks if the input string is an integer
            if (isDouble(testInput))
            {   
                parsedInput = true;
                double newInput = Double.parseDouble(testInput);               
                return newInput;
            }
        }
           
        return 0;
    }
    
    /**
     * Create a new customer 
     */
    private void createCustomer()
    {   
        // customer discount
        double discount = 0;
        
        // checks if the user inputs a value higher than max discount for a customer
        boolean checkMaxDiscount = false;
        
        System.out.println("Indtast navn");
        String name = inputString();
        
        System.out.println("Indtast adresse");
        String address = inputString();
        
        System.out.println("Indtast Tlf nr.");
        String phone = inputString();
        
        System.out.println("Indtast E-mail");
        String email = inputString();
        
        System.out.println("Indtast ID");
        String customerID = inputString();
        
        // max discount is 20% 
        do
        {
            System.out.println("Indtast rabat som procent som decimaltal (eksempel: 0.10) (max er 0.20)");
            discount = inputDouble();
            
            if (discount > 0.20 || discount < 0)
            {
                System.out.println("Du skal indtaste en rabat som er mindre end 20 og større end 0");                
            }
            else
            {
                checkMaxDiscount = true;
            }
        }
        while (!checkMaxDiscount);
        
        System.out.println("Indtast Cvr nr.");
        String cvrNr = inputString();
        
        if (!name.isEmpty() && !address.isEmpty() && !phone.isEmpty() && !email.isEmpty() && 
            !customerID.isEmpty() && !(discount < 0) && !cvrNr.isEmpty())
        {
            try
            {   
                if (personCtr.getCustomer(customerID) == null)
                {
                    personCtr.addCustomer(name, address, phone, email, customerID, discount, cvrNr);
                    System.out.println("Kunden " + name + " med id: " + customerID + " er tilføjet");
                    waitInput();
                }
                else 
                {
                    System.out.println("Kunden findes allerede i systemet");
                }
            }
            catch(NullPointerException e )
            {
                System.out.println("Du har en eller flere værdier som ikke er gyldige for oprettelse af en kunde");
                waitInput();
            }
        }
        else
        {
            System.out.println("Du har en eller flere værdier som ikke er gyldige for oprettelse af en kunde");
            waitInput();
        }
    }
    
    /**
     * Find an customer
     */
    private void findCustomer()
    {
        System.out.println("Indtast kundens ID");
        String searchString = inputString();
        
        if (searchString != null)
        {  
            try
            {
                System.out.println("");
                System.out.println("Navn: " + personCtr.getCustomer(searchString).getName());
                System.out.println("Adresse: " + personCtr.getCustomer(searchString).getAddress());
                System.out.println("Tlf nr.: " + personCtr.getCustomer(searchString).getPhone());
                System.out.println("E-mail: " + personCtr.getCustomer(searchString).getEmail());
                System.out.println("ID: " + personCtr.getCustomer(searchString).getCustomerID());
                System.out.println("Rabat: " + personCtr.getCustomer(searchString).getDiscount());
                System.out.println("Cvr: " + personCtr.getCustomer(searchString).getCvrNr());
                waitInput();
            }
            catch(NullPointerException e )
            {
                System.out.println("Der skete en fejl, da programmet prøvede at finde kunden");
                System.out.println("(Måske er kunden ikke oprettet i systemet)");
                System.out.println("");
                waitInput();
            }
        }
        else 
        {
            System.out.println("Du skal indtaste et id for at finde en kunde");
            waitInput();
        }
        
    }
    
    /**
     * Delete a customer
     */
    private void deleteCustomer()
    {
        System.out.println("Indtast ID på den kunde som skal slettes");
        String searchID = inputString();
        
        if (searchID != null)
        {  
            try
            {   
                personCtr.deleteCustomer(searchID);
                waitInput();
            }
            catch(IndexOutOfBoundsException e)
            {
                System.out.println("System kunne ikke finde og slette kunden");
                System.out.println("(Måske er kunden ikke registeret?");
                waitInput();
            }
        }
        else
        {
            System.out.println("Du skal indtaste et ID for at kunne slette en kunde");
            waitInput();
        }   
    }
    
    /**
     * Update a customer
     */
    private void updateCustomer()
    {
        System.out.println("Indtast ID på den kunde som skal opdateres");
        String ID = inputString();
        
        System.out.println("Indtast navn");
        String name = inputString();
        
        System.out.println("Indtast adresse");
        String address = inputString();
        
        System.out.println("Indtast Tlf nummer");
        String phone = inputString();
        
        System.out.println("Indtast E-mail");
        String email = inputString();
        
        System.out.println("Indtast nye ID");
        String customerID = inputString();
        
        System.out.println("Indtast Rabat");
        double discount = inputDouble();
        
        System.out.println("Indtast CVR");
        String cvrNr = inputString();
        
        if (personCtr.getCustomer(ID) != null)
        {   
            try
            {
                personCtr.updateCustomer(name, address, phone, email, customerID, discount, cvrNr, ID);
                System.out.println("Kunden " + name + " med CVR nummeret: " + cvrNr + " og id: " + customerID + 
                                   " blev opdateret");
                waitInput();
            }
            catch(NullPointerException e)
            {
                System.out.println("Du har ikke indtaste alle værdierne, som skal bruges til at opdatere enkunde i systemet");
                waitInput();
            }
        
        }
        else
        {
            System.out.println("Den kunde som skulle opdateres kunne ikke findes i systemet");
            waitInput();
        }
    }
   
    /**
     * Show all customers
     */
    private void showAllCustomers()
    {
        try 
        { 
            personCtr.listAllCustomers();
            waitInput();
        }
        catch(NullPointerException e)
        {
            System.out.println("Der skete en fejl, da systemet prøvede at liste alle registered kunder");
            waitInput();
        }
    }
}
